// <copyright file="LayoutUnit.cs" company="YUIAsp.Net">
// Copyright (c) 2009 All Right Reserved
// </copyright>
// <author>raul popescu</author>
// <email>dev@yuiasp.net</email>
// <date>2009-01-30</date>
// <summary>YUIAsp.Net File</summary>

using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace YUIAspNet
{
    /// <summary>
    /// A unit that can be contained inside a LayoutManager.
    /// </summary>
    [ToolboxItem(false)]
    [ParseChildren(true)]
    public class LayoutUnit : WebControl, INamingContainer
    {
        private Panel _pnlHeader;
        private Panel _pnlContent;
        private Panel _pnlFooter;

        /// <summary>
        /// Initializes a new instance of the <see cref="LayoutUnit"/> class.
        /// </summary>
        public LayoutUnit() : base("div")
        {
            this.Init += new EventHandler(LayoutUnit_Init);

            _pnlHeader = new Panel();
            _pnlFooter = new Panel();
            _pnlContent = new Panel();
        }

        /// <summary>
        /// Gets the header container.
        /// </summary>
        /// <value>The header container.</value>
        public Panel Header
        {
            get { return _pnlHeader; }
        }

        /// <summary>
        /// Gets the content container.
        /// </summary>
        /// <value>The content container.</value>
        public Panel Content 
        {
            get { return _pnlContent; }
        }


        /// <summary>
        /// Gets the footer container.
        /// </summary>
        /// <value>The footer container.</value>
        public Panel Footer
        {
            get { return _pnlFooter; }
        }

        /// <summary>
        /// Called by the ASP.NET page framework to notify server controls that use composition-based implementation to create any child controls they contain in preparation for posting back or rendering.
        /// </summary>
        protected override void CreateChildControls()
        {
            if (HeaderTemplate != null)
            {
                _pnlHeader.ID = "top";
                Controls.Add(_pnlHeader);
                HeaderTemplate.InstantiateIn(_pnlHeader);
                _pnlHeader.Style.Add("display", "none");
            }

            if (ContentTemplate != null)
            {
                _pnlContent.ID = "content";
                Controls.Add(_pnlContent);
                ContentTemplate.InstantiateIn(_pnlContent);
            }

            if (FooterTemplate != null)
            {
                _pnlFooter.ID = "footer";
                Controls.Add(_pnlFooter);
                FooterTemplate.InstantiateIn(_pnlFooter);
            }

            base.CreateChildControls();
        }

        void LayoutUnit_Init(object sender, EventArgs e)
        {
            EnsureChildControls();
        }

        /// <summary>
        /// Gets or sets the content template.
        /// </summary>
        /// <value>The content template.</value>
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [TemplateInstance(TemplateInstance.Single)]
        [Browsable(false)]
        [MergableProperty(false)]
        public ITemplate ContentTemplate { get; set; }

        /// <summary>
        /// Gets or sets the header template.
        /// </summary>
        /// <value>The header template.</value>
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [TemplateInstance(TemplateInstance.Single)]
        [Browsable(false)]
        [MergableProperty(false)]
        public ITemplate HeaderTemplate { get; set; }

        /// <summary>
        /// Gets or sets the footer template.
        /// </summary>
        /// <value>The footer template.</value>
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [TemplateInstance(TemplateInstance.Single)]
        [Browsable(false)]
        [MergableProperty(false)]
        public ITemplate FooterTemplate { get; set; }

        /// <summary>
        /// Gets or sets the width of the Web server control.
        /// </summary>
        /// <value></value>
        /// <returns>A <see cref="T:System.Web.UI.WebControls.Unit"/> that represents the width of the control. The default is <see cref="F:System.Web.UI.WebControls.Unit.Empty"/>.</returns>
        /// <exception cref="T:System.ArgumentException">The width of the Web server control was set to a negative value. </exception>
        public new int Width { get; set; }

        /// <summary>
        /// Gets or sets the width of the min.
        /// </summary>
        /// <value>The width of the min.</value>
        public int MinWidth { get; set; }

        /// <summary>
        /// Gets or sets the width of the max.
        /// </summary>
        /// <value>The width of the max.</value>
        public int MaxWidth { get; set; }

        /// <summary>
        /// Gets or sets the height of the Web server control.
        /// </summary>
        /// <value></value>
        /// <returns>A <see cref="T:System.Web.UI.WebControls.Unit"/> that represents the height of the control. The default is <see cref="F:System.Web.UI.WebControls.Unit.Empty"/>.</returns>
        /// <exception cref="T:System.ArgumentException">The height was set to a negative value.</exception>
        public new int Height { get; set; }

        /// <summary>
        /// Gets or sets the height of the min.
        /// </summary>
        /// <value>The height of the min.</value>
        public int MinHeight { get; set; }

        /// <summary>
        /// Gets or sets the height of the max.
        /// </summary>
        /// <value>The height of the max.</value>
        public int MaxHeight { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="LayoutUnit"/> is animate.
        /// </summary>
        /// <value><c>true</c> if animate; otherwise, <c>false</c>.</value>
        public bool Animate { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="LayoutUnit"/> is close.
        /// </summary>
        /// <value><c>true</c> if close; otherwise, <c>false</c>.</value>
        public bool Close { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="LayoutUnit"/> is collapse.
        /// </summary>
        /// <value><c>true</c> if collapse; otherwise, <c>false</c>.</value>
        public bool Collapse { get; set; }

        /// <summary>
        /// Gets or sets the size of the collapse.
        /// </summary>
        /// <value>The size of the collapse.</value>
        public int CollapseSize { get; set; }

        /// <summary>
        /// Gets or sets the data URL.
        /// </summary>
        /// <value>The data URL.</value>
        public Uri DataUrl { get; set; }

        /// <summary>
        /// Gets or sets the position.
        /// </summary>
        /// <value>The position.</value>
        public LayoutPosition Position { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="LayoutUnit"/> is resizeable.
        /// </summary>
        /// <value><c>true</c> if resizeable; otherwise, <c>false</c>.</value>
        public bool Resize { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="LayoutUnit"/> is scrollable.
        /// </summary>
        /// <value><c>true</c> if scrollable; otherwise, <c>false</c>.</value>
        public bool? Scroll { get; set; }

        /// <summary>
        /// Gets or sets the z-index.
        /// </summary>
        /// <value>The z-index.</value>
        public int ZIndex { get; set; }
    }
}
